﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StudyMaintenanceDomainService.cs" company="ELEN 7045 GROUP II">
//    Contributed by E.Vosloo
// </copyright>
// <summary>
//   The study maintenance domain service
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Domain.StudyDefinition.Services
{
    #region

    using System;

    using Domain.StudyDefinition.Contracts.DomainServices;
    using Domain.StudyDefinition.Contracts.Repositories.Studies;
    using Domain.StudyDefinition.Contracts.Repositories.SubjectGroups;
    using Domain.StudyDefinition.Contracts.Repositories.Treatments;
    using Domain.StudyDefinition.Entities;

    #endregion

    /// <summary>
    /// The study maintenance domain service
    /// </summary>
    public class StudyMaintenanceDomainService : IStudyMaintenanceDomainService
    {
        #region Constants and Fields

        /// <summary>
        /// The IStudyRepository Instance to use in this service.
        /// </summary>
        private readonly IStudyRepository studyRepositoryInstance;

        /// <summary>
        /// The ISubjectGroupRepository Instance to use in this service.
        /// </summary>
        private readonly ISubjectGroupRepository subjectGroupRepositoryInstance;

        /// <summary>
        /// The ITreatmentRepository Instance to use in this service.
        /// </summary>
        private readonly ITreatmentRepository treatmentRepositoryInstance;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="StudyMaintenanceDomainService"/> class.
        /// </summary>
        /// <param name="studyRepository">
        /// The study repository.
        /// </param>
        /// <param name="subjectGroupRepository">
        /// The subject group repository.
        /// </param>
        /// <param name="treatmentRepository">
        /// The treatment repository.
        /// </param>
        public StudyMaintenanceDomainService(
            IStudyRepository studyRepository, 
            ISubjectGroupRepository subjectGroupRepository, 
            ITreatmentRepository treatmentRepository)
        {
            if (studyRepository == null)
            {
                throw new ArgumentNullException("studyRepository");
            }

            if (subjectGroupRepository == null)
            {
                throw new ArgumentNullException("subjectGroupRepository");
            }

            if (treatmentRepository == null)
            {
                throw new ArgumentNullException("treatmentRepository");
            }

            this.studyRepositoryInstance = studyRepository;
            this.subjectGroupRepositoryInstance = subjectGroupRepository;
            this.treatmentRepositoryInstance = treatmentRepository;
        }

        #endregion

        #region Implemented Interfaces

        #region IStudyMaintenanceDomainService

        /// <summary>
        /// Archives the specified study.
        /// </summary>
        /// <param name="study">
        /// The study to archive.
        /// </param>
        public void ArchiveStudy(Study study)
        {
            study.ArchiveStudy();

            this.studyRepositoryInstance.Update(study);
        }

        /// <summary>
        /// Deletes the specified study.
        /// </summary>
        /// <param name="study">
        /// The study to delete.
        /// </param>
        public void DeleteStudy(Study study)
        {
            foreach (var treatment in study.Treatments)
            {
                foreach (var subjectGroup in treatment.SubjectGroups)
                {
                    this.subjectGroupRepositoryInstance.Delete(subjectGroup);
                }

                this.treatmentRepositoryInstance.Delete(treatment);
            }

            this.studyRepositoryInstance.Delete(study);
        }

        /// <summary>
        /// Locks the study.
        /// </summary>
        /// <param name="study">
        /// The study.
        /// </param>
        public void LockStudy(Study study)
        {
            study.LockStudy();
            this.studyRepositoryInstance.Update(study);
        }

        /// <summary>
        /// Unlocks the study.
        /// </summary>
        /// <param name="study">
        /// The study.
        /// </param>
        public void UnlockStudy(Study study)
        {
            study.UnLockStudy();

            this.studyRepositoryInstance.Update(study);
        }

        /// <summary>
        /// Updates the specified study.
        /// </summary>
        /// <param name="study">
        /// The study to update.
        /// </param>
        public void UpdateStudy(Study study)
        {
            study.UpdateModifiedDate();

            this.studyRepositoryInstance.Update(study);
        }

        #endregion

        #endregion
    }
}